package com.um.swing.core;

import cn.hutool.core.img.GraphicsUtil;
import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.RandomUtil;
import com.um.swing.ThreadPool;
import com.um.swing.bean.ImageItem;
import com.um.swing.common.util.ImageUtil;
import com.um.swing.common.util.MathUtils;
import com.um.swing.core.event.ExportEvent;
import com.um.swing.core.thread.ExportManageRunable;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * 创建人：袁泉
 * 创建时间：2022年04月25日22:24:26
 * 描述：图书拼接核心功能
 */
public class ImageJoinCore {

    /**
     * 古书拼接算法
     *
     * @param item
     * @param prvItem
     * @param observer
     * @return
     */
    public static Image imageJoin(ImageItem item, ImageItem prvItem, ImageObserver observer) {
        try {
            boolean prvItemIsLeft = MathUtils.point4IsLeft(prvItem);
            ImageItem leftItem = prvItemIsLeft ? prvItem : item;
            ImageItem rightItem = prvItemIsLeft ? item : prvItem;

            BufferedImage imgRight = cutImage(rightItem);
            BufferedImage imgLeft = cutImage(leftItem);
//        ImgUtil.write(imgRight, new File("/Users/enizumi/work/古书/cs/cs/" + RandomUtil.randomNumbers(5) + ".png"));

            int imgRightTop = NumberUtil.min(rightItem.getPointMapItem(ImageItem.P3).y, rightItem.getPointMapItem(ImageItem.P4).y);
            int imgRightBottom = NumberUtil.max(rightItem.getPointMapItem(ImageItem.P3).y, rightItem.getPointMapItem(ImageItem.P4).y);

            int imgLeftTop = NumberUtil.min(leftItem.getPointMapItem(ImageItem.P3).y, leftItem.getPointMapItem(ImageItem.P4).y);
            int imgLeftBottom = NumberUtil.max(leftItem.getPointMapItem(ImageItem.P3).y, leftItem.getPointMapItem(ImageItem.P4).y);

            int width = imgRight.getWidth(observer) + imgLeft.getWidth(observer);
            int height = imgLeft.getHeight(observer);
            BufferedImage tImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            Graphics2D graphics = GraphicsUtil.createGraphics(tImg, Color.WHITE);
            graphics.drawImage(imgLeft, 0, 0, observer);

//            ImgUtil.write(tImg, new File("/Users/enizumi/work/古书/cs/cs/" + RandomUtil.randomNumbers(5) + ".png"));

            //计算第二张的Y坐标
            int m2y = imgLeftTop - imgRightTop;
            //左右版面高度差
            int cc = (imgLeftBottom - imgLeftTop) - (imgRightBottom - imgRightTop);
            Image cut1 = ImgUtil.cut(imgRight, new Rectangle(0, 0, imgRight.getWidth(), imgRightTop));
            Image cut2 = ImgUtil.cut(imgRight, new Rectangle(0, imgRightTop, imgRight.getWidth(), imgRight.getHeight()));
            graphics.drawImage(cut1, imgLeft.getWidth(), m2y, cut1.getWidth(observer), cut1.getHeight(observer), observer);
            graphics.drawImage(cut2, imgLeft.getWidth(), cut1.getHeight(observer) + m2y, cut2.getWidth(observer), cut2.getHeight(observer) + cc, observer);

            //中线
            //TODO:测试用
//            graphics.setColor(Color.RED);
//            graphics.setStroke(new BasicStroke(5));
//            graphics.drawLine(imgLeft.getWidth(observer), 0, imgLeft.getWidth(observer), imgLeft.getHeight(observer));
            graphics.dispose();

//            ImgUtil.write(tImg, new File("/Users/enizumi/work/古书/cs/cs/" + RandomUtil.randomNumbers(5) + ".png"));


            //图片裁剪逻辑
            Point rightTopPoint = rightItem.sideTopPoint();
            Rectangle rect = new Rectangle();
            rect.x = 0;
            rect.width = imgLeft.getWidth() + imgRight.getWidth() - rightTopPoint.x - 100;
            rect.y = 0;//Math.max(imgLeftTop - 50, 0);
            rect.height =  tImg.getHeight();//Math.min(imgLeftBottom - imgLeftTop + 100, tImg.getHeight());

            Image img = ImgUtil.cut(tImg, rect);
            return img;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    private static BufferedImage cutImage(ImageItem prvItem) throws IOException {
        Point p1 = prvItem.getPointMapItem(ImageItem.P1);
        Point p2 = prvItem.getPointMapItem(ImageItem.P2);
        Point p3 = prvItem.getPointMapItem(ImageItem.P3);
        Point p4 = prvItem.getPointMapItem(ImageItem.P4);

        BufferedImage img2 = ImageIO.read(new File(prvItem.getPath()));
        //旋转扶正
        Double angle = MathUtils.getAngle(p1, p2);
        double theta = Math.floor(90 - angle + 0.5);
        Graphics2D graphics = (Graphics2D) img2.getGraphics();
        //判断方向
        boolean isToLeft = MathUtils.point4IsLeft(prvItem);//是否朝着左边
        Point topLineP1 = new Point(0, 0);
        Point topLineP2 = new Point(img2.getWidth(), 0);

        Point bottomLineP1 = new Point(0, img2.getHeight());
        Point bottomLineP2 = new Point(img2.getWidth(), img2.getHeight());

        Point dP1 = MathUtils.getIntersectPoint(p1, p2, topLineP1, topLineP2);
        Point dP2 = MathUtils.getIntersectPoint(p1, p2, bottomLineP1, bottomLineP2);
        Point topPoint = prvItem.lineTopPoint();
        Point topP1 = MathUtils.getIntersectPoint(new Point(0, topPoint.y), topPoint, dP1, dP2);//顶部点和边线的交点
//        graphics.setColor(isToLeft?Color.RED:Color.BLUE);
//        graphics.fillOval(topP1.x + (isToLeft ? -0:0),topP1.y,20,20);
//        graphics.drawLine(dP1.x, dP1.y, dP2.x, dP2.y);
        int x[];
        int y[];
        //进行裁剪
        if (isToLeft) {
            x = new int[]{dP1.x, topLineP1.x, bottomLineP1.x, dP2.x, dP1.x};
            y = new int[]{dP1.y, topLineP1.y, bottomLineP1.y, dP2.y, dP1.y};
        } else {
            x = new int[]{dP1.x, topLineP2.x, bottomLineP2.x, dP2.x, dP1.x};
            y = new int[]{dP1.y, topLineP2.y, bottomLineP2.y, dP2.y, dP1.y};
        }
        //裁剪
        img2 = ImageUtil.cutPolygon_Image((BufferedImage) img2, x, y, 5);

        //旋转
        img2 = ImageUtil.modifyImageRatio((BufferedImage) img2, 90 - angle, topP1);

        Rectangle cutRectangle = null;
        if (isToLeft) {
            //x值变成了img2的宽度
//            cutRectangle = new Rectangle(0, 0, p1.x - 1, img2.getHeight());
            cutRectangle = new Rectangle(0, 0, topP1.x, img2.getHeight());
        } else {
            cutRectangle = new Rectangle(topP1.x, 0, img2.getWidth(), img2.getHeight());
        }
        Image cut = ImgUtil.cut(img2, cutRectangle);
        BufferedImage resImg = ImgUtil.copyImage(cut, BufferedImage.TYPE_INT_ARGB, new Color(255, 255, 255, 0));
//        if(!isToLeft){
//            ImgUtil.write(cut, new File("/Users/enizumi/work/古书/cs/" + RandomUtil.randomNumbers(5) + "-.png"));
//            ImgUtil.write(resImg, new File("/Users/enizumi/work/古书/cs/" + RandomUtil.randomNumbers(5) + ".png"));
//
//        }
        return resImg;
    }


    public static ExportManageRunable exportJoinResult(File targetFile, List<ImageItem> mImageList, ExportEvent callback) {
        ExportManageRunable runable = new ExportManageRunable(mImageList, targetFile.getPath(), callback);
        ThreadPool.execute(runable);
        return runable;
    }
}
